package  com.lay.gui;


import javax.swing.*;

import com.tools.CpuTools;

import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class AxisFrame extends JFrame {
private static CpuTools  cpuTools=new  CpuTools();
    public AxisFrame() throws HeadlessException, InterruptedException {
        super("坐标图示例");
        this.design();
        startDataThread();
        startAutoThread();
        Thread.sleep(2000);
        startGetFreqThread();
        
        }

   public static  AxisCanvas canvas;

    private void design() {
        Container content = this.getContentPane();
        content.setLayout(new BorderLayout());
        canvas=new AxisCanvas();
        for(int i=0;i<canvas.data.length;i++)canvas.data[i]=-10;
        content.add(canvas,BorderLayout.CENTER);
    }

    public static void main(String[] args) throws Exception {
    	
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        AxisFrame frame=new AxisFrame();
        frame.setBounds(100,100,1100,480);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setBackground(Color.BLACK);
    }


    
public  static void  startAutoThread(){
    	
    	new Thread()	{
    		@Override
    		public void run() {
    				cpuTools.autoCpuFreq();
    		}
    	}.start();
    }    
    
    

public  static void  startDataThread(){
	
	
	new Thread()	{
		@Override
		public void run() {
			
			while(true){
				double idle=cpuTools.testCpuFreq();
				System.out.print(" "+idle);
				canvas.drawMyData((int)cpuTools.testCpuFreq());
			try {
				Thread.sleep(5);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		}
	}.start();
}



public  static void  startGetFreqThread(){
	
	new Thread()	{
		@Override
		public void run() {
			while(true){
				double freq=cpuTools.getCpuFreq(0);
				System.out.print(" "+freq);
				canvas.drawFreqData((int)cpuTools.getCpuFreq(0));
			try {
				Thread.sleep(2050);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		}
	}.start();
}

class AxisCanvas extends JLabel {

    private Insets borderInsets=new Insets(20,30,30,20);
    private String xLabel="CpuIdle";
    private String yLabel="Time";
    private String xFreq="CpuFreq";
    private String yFreq="Time";
    	private String freq3="2300000";
    	private String freq2="1600000";
    	private String freq1="1200000";
    	private String idle20="20%";
    	private String idle40="40%";
    	private String idle60="60%";
    	private String idle80="80%";
    	private String idle100="100%";
    	private String timeString;
    public  Graphics  graphics;
    public int  []  data=new  int[20];
    public int [] data2= new int[20];
    int  index=0;
    int index2=0;

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        
        
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式yyyy-MM-dd HH:mm:ss
        	timeString = df.format(new Date());
        	System.out.println(timeString);// new Date()为获取当前系统时间
//
        g.setColor(this.getForeground());
        g.drawLine(borderInsets.left-27,borderInsets.top-17,borderInsets.left-27, 200);
        g.drawLine(borderInsets.left-27,borderInsets.top-17,borderInsets.left-30,borderInsets.top-7);
        g.drawLine(borderInsets.left-27,borderInsets.top-17,borderInsets.left-24,borderInsets.top-7);
        g.drawLine(borderInsets.left-27,200, 1000,200);
        
        g.drawLine(borderInsets.left-27,2*(100-100)+3,borderInsets.left-22,2*(100-100)+3);
        g.drawLine(borderInsets.left-27,2*(100-80)+3,borderInsets.left-22,2*(100-80)+3);
        g.drawLine(borderInsets.left-27,2*(100-60)+3,borderInsets.left-22,2*(100-60)+3);
        g.drawLine(borderInsets.left-27,2*(100-40)+3,borderInsets.left-22,2*(100-40)+3);
        g.drawLine(borderInsets.left-27,2*(100-20)+3,borderInsets.left-22,2*(100-20)+3);
        
        g.drawLine(1000,200,1000-5,200-3);
        g.drawLine(1000,200,1000-5,200+3);
        g.setFont(this.getFont());
        g.drawString(xLabel,50,15);
        g.drawString(yLabel,1000,200);
        
        g.drawString(idle20,borderInsets.left-20,2*(100-20)+13);
        g.drawString(idle40,borderInsets.left-20,2*(100-40)+13);
        g.drawString(idle60,borderInsets.left-20,2*(100-60)+13);
        g.drawString(idle80,borderInsets.left-20,2*(100-80)+13);
        g.drawString(idle100,borderInsets.left-20,2*(100-100)+13);
//    

        
        g.drawLine(borderInsets.left-27,(borderInsets.top-12)+200,borderInsets.left-27, 400);
        g.drawLine(borderInsets.left-27,(borderInsets.top-12)+200,borderInsets.left-30,(borderInsets.top-7)+200);
        g.drawLine(borderInsets.left-27,(borderInsets.top-12)+200,borderInsets.left-24,(borderInsets.top-7)+200);
        g.drawLine(borderInsets.left-27,400, 1000,400);
        
        g.drawLine(borderInsets.left-27,450-(2300000/100000)*10,borderInsets.left-22,450-(2300000/100000)*10);
        g.drawLine(borderInsets.left-27,450-(1600000/100000)*10,borderInsets.left-22,450-(1600000/100000)*10);
        g.drawLine(borderInsets.left-27,450-(1200000/100000)*10,borderInsets.left-22,450-(1200000/100000)*10);
        g.drawLine(1000,400,1000-5,400-3);
        g.drawLine(1000,400,1000-5,400+3);
        g.setFont(this.getFont());
        g.drawString(xFreq,10,15+200);
        g.drawString(yFreq,1000,400);
        g.drawString(freq3,borderInsets.left-20,450-(2300000/100000)*10+10);
        g.drawString(freq2,borderInsets.left-20,450-(1600000/100000)*10+10);
        g.drawString(freq1,borderInsets.left-20,450-(1200000/100000)*10+10);
//    
        
        
        Graphics2D g2d= (Graphics2D) g;
        this.graphics=g2d;
        g2d.setColor(Color.green);
              			
        		for(int  i=0,j=0;i<20;i++){
        					if(i!=0 && data[i]>0){
        						g2d.fillOval(j, 2*(100-data[i]), 5, 5);
        						graphics.drawLine(j-50, 2*(100-data[i-1])+3, j, 2*(100-data[i])+3);
        						j+=50;
        					}			
        		}
        		
        Graphics2D gtd= (Graphics2D) g;
        this.graphics=gtd;
        gtd.setColor(Color.green);
              			
        		for(int  i=0,j=0;i<20;i++){
        					if(i!=0 && data2[i]>0){
        						gtd.fillOval(j, 450-(data2[i]/100000)*10, 5, 5);
        						graphics.drawLine(j-50, 450-(data2[i-1]/100000)*10, j, 450-(data2[i]/100000)*10);
        						j+=50;
        					}			
        		}
      g.drawString(timeString,1000,220);
      g.drawString(timeString,1000,420);
    }

    
    
public  void  drawMyData(int idle){
    	if(index<20){
    		index++;
    		}
    	else{
    	//	index=0;
    		
    		for(int i=0;i<19;i++)
    			{
    				data[i]=data[i+1];
    			}
    	}
    	if((idle <= 0) && (index >2))
					idle = data[index-2];
    	data[index-1]=idle;
    			
    	repaint();
    	
    }
    
    
public  void  drawFreqData(int freq){
    	if(index2<20){
    		index2++;
    		}
    	else{
 //   		index2=0;
		    	for(int i=0;i<19;i++)
					{
						data2[i]=data2[i+1];
					}
    	
    	}
    	if(freq<120000)
    			freq = 1200000;
    	data2[index2-1]=freq;
    	
    	repaint();
    	
    }
}

}